﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// using these 
// references to add System.Printing, ReachFramework
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

// references to add System.Drawing
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

namespace Photo_Taking
{
    class PhotoTake
    {

        public void PrintSomethingNew(string fileName)
        {
            PrintDialog dialog = new PrintDialog();
            //if (dialog.ShowDialog() != true)
            //{ return; }

            //sets the orientation of the printing
            //dialog.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape;
            dialog.PrintTicket.PageOrientation = System.Printing.PageOrientation.Portrait;


            //get selected printer capabilities
            System.Printing.PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);

            //defines the border of the printing
            dialog.PrintTicket.PageBorderless = System.Printing.PageBorderless.None;
            //dialog.PrintTicket.PageScalingFactor = System.Printing.PageScalingFactorRange.

            // Open a Stream and decode a PNG image
            Stream imageStreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];


            System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
            myImage.Source = bitmapSource;
            myImage.Stretch = Stretch.UniformToFill;
            myImage.Stretch = Stretch.None;

            //myImage .MinHeight etc will be 0 if not given any values
            //we need to set the values in, so that there would not be a nullpointer exception when we are using System.Windows.Size later
            myImage.MinHeight = bitmapSource.Height;
            myImage.MinWidth = bitmapSource.Width;
            myImage.Height = bitmapSource.Height;
            myImage.Width = bitmapSource.Width;

            double printHeight = 354.6;
            double printWidth = 498;


            //get scale of the print with reference to image
            /*double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / myImage.Width,
            capabilities.PageImageableArea.ExtentHeight /
            myImage.Height);*/

            double scale = Math.Min(printWidth / myImage.Width, printHeight / myImage.Height);


            Console.WriteLine("testing scale:" + scale);

            //Transform the Visual to scale
            myImage.LayoutTransform = new ScaleTransform(scale, scale);


            //get the size of the printer page
            System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
            sz.Height = printHeight;
            sz.Width = printWidth;
   

            //reading from the size of the printer page, the image is being scaled to the correct size to be printed
            myImage.Measure(sz);
            //myImage.Arrange(new Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
            myImage.Arrange(new Rect(new System.Windows.Point(34, 34), sz));

            Console.WriteLine(capabilities.PageImageableArea.OriginHeight);
            Console.WriteLine(capabilities.PageImageableArea.OriginWidth); 

            dialog.PrintVisual(myImage, "A Great Image. " + fileName);
            Console.WriteLine("printing a new picture");
        }



        /// Creates an Image object containing a screen shot of the entire desktop
        /// </summary>
        /// <returns></returns>
        public System.Drawing.Image CaptureScreen()
        {
            return CaptureWindow(User32.GetDesktopWindow());
        }

        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        public System.Drawing.Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up 
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            // get a .NET image object for it
            System.Drawing.Image img = System.Drawing.Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);

            return img;
        }

        /// <summary>
        /// Captures a screen shot of a specific window, and saves it to a file
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
        {
            System.Drawing.Image img = CaptureWindow(handle);
            img.Save(filename, format);
        }

        /// <summary>
        /// Captures a screen shot of the entire desktop, and saves it to a file
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureScreenToFile(string filename, ImageFormat format)
        {
            System.Drawing.Image img = CaptureScreen();
            img.Save(filename, format);
            //PrintSomethingNew(filename);
        }

        /// <summary>
        /// Helper class containing Gdi32 API functions
        /// </summary>
        private class GDI32
        {

            public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter

            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
                int nWidth, int nHeight, IntPtr hObjectSource,
                int nXSrc, int nYSrc, int dwRop);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
                int nHeight);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll")]
            public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
        }

        /// <summary>
        /// Helper class containing User32 API functions
        /// </summary>
        private class User32
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }

            [DllImport("user32.dll")]
            public static extern IntPtr GetDesktopWindow();
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowDC(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);

        }



    }

}
